/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.kernel;

import java.util.HashSet;
import java.util.Set;

import org.openaecfm.model.kernel.ObjectDefinition;
import org.openaecfm.model.kernel.PropertySetDefinition;
import org.openaecfm.model.kernel.RelDefinesByProperties;
import org.openaecfm.model.kernel.Root;
import org.openaecfm.service.kernel.IRelDefinesByPropertiesManager;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class RelDefinesByPropertiesIO extends
		AbstractEntityInstanceInputImpl<RelDefinesByProperties> implements
		IEntityInstanceUtilInput<RelDefinesByProperties>,
		IEntityInstanceOutput<RelDefinesByProperties>,
		IEntityInstanceUtilOutput<RelDefinesByProperties> {
	private IEntityInstanceUtilInput<Root> utilInput;
	private IEntityInstanceUtilOutput<Root> utilOutput;
	private IRelDefinesByPropertiesManager manager;

	public void setManager(IRelDefinesByPropertiesManager manager) {
		this.manager = manager;
	}

	public void setUtilOutput(IEntityInstanceUtilOutput<Root> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(RelDefinesByProperties pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCRELDEFINESBYPROPERTIES", appDataManager.getName(pojo));

		exportSPF(pojo, ei, appDataManager);

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected RelDefinesByProperties doCreateObject() {
		return new RelDefinesByProperties();
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void exportSPF(RelDefinesByProperties pojo, EntityInstance ei,
			IAppDataManager appDataManager) {
		utilOutput.exportSPF(pojo, ei, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set RelatedObjects
		ListParameter relatedObjects = new ListParameter();
		for (ObjectDefinition o : pojo.getRelatedObjects()) {
			relatedObjects.add(new EntityInstanceNameParameter(appDataManager
					.getName(o)));
		}
		sei.addParameter(relatedObjects);

		// Set RelatingPropertyDefinition
		if (pojo.getRelatingPropertyDefinition() instanceof PropertySetDefinition) {
			sei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getRelatingPropertyDefinition())));
		} else if (pojo.getRelatingPropertyDefinition() instanceof Set<?>) {
			ListParameter l = new ListParameter();
			Set<PropertySetDefinition> ss = (Set<PropertySetDefinition>) pojo
					.getRelatingPropertyDefinition();
			for (Object o : ss) {
				l.add(new EntityInstanceNameParameter(appDataManager.getName(o)));
			}
			sei.addParameter(l);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveNotReferenceParameter(EntityInstance ei,
			RelDefinesByProperties pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);
	}

	public void setUtilInput(IEntityInstanceUtilInput<Root> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			RelDefinesByProperties pojo, IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set RelatedObjects
		ListParameter relatedObjects = (ListParameter) sei.getParameter(4);
		for (Parameter p : relatedObjects) {
			EntityInstanceNameParameter e = (EntityInstanceNameParameter) p;
			manager.addRelatedObject(pojo, (ObjectDefinition) appDataManager
					.getObject(e.toInstanceName()));
		}

		// Set RelatingPropertyDefinition
		Parameter rtingpd = sei.getParameter(5);
		if (rtingpd.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter e = (EntityInstanceNameParameter) rtingpd;
			manager.setRelatingPropertyDefinition(pojo,
					appDataManager.getObject(e.toInstanceName()));
		} else if (rtingpd.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) rtingpd;
			Set<PropertySetDefinition> hs = new HashSet<PropertySetDefinition>();
			for (Parameter p : l) {
				EntityInstanceNameParameter e = (EntityInstanceNameParameter) p;
				hs.add((PropertySetDefinition) appDataManager.getObject(e
						.toInstanceName()));
			}
			pojo.setRelatingPropertyDefinition(hs);
		}
	}
}
